import request from '@/utils/request'
import { getToken } from '@/utils/auth' // get token from cookie
import Axios from 'axios'
export function upload(data) {
  return request({
    url: '/attachment/upload',
    method: 'post',
    data,
    dataType: 'JSON',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
  })
}

// export function download(id) {
//   return request({
//     url: `/attachment/${id}/download`
//   })
// }
export function download(id) {
  const queryArgs = {
    url: `${import.meta.env.VITE_APP_BASE_API}/attachment/${id}/download`,
    method: 'get',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json; charset=utf-8',
      withCredentials: true,
      token: getToken()
    },
    responseType: 'arraybuffer'
  }
  // tips: 这里直接返回的是response整体!
  return Axios.request(queryArgs).catch(err => err)
}

export function readImg(id) {
  return request({
    url: `/attachment/${id}/img`
  })
}
